<?php

class Admin_Form_ValidateProductCategories extends Zendvn_Validate_Form{
	
	public function __construct($arrParam = null, $options = null){
		define('NAME_ID',$arrParam['id']);
		$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
												'fileConfig'=>'validates/product-category.ini'));
		if($arrParam['action']=='edit'){
			$config = new Zendvn_Controller_Config(array('module'=>$arrParam['module'],
														'fileConfig'=>'validates/product-category.ini',
														'section'=>'edit'));
		}
		//==========================Validate name============================
		$name  			= $config->getConfig('name');
		$validator  	=  $this->validate($name,$arrParam);
		if(!$validator->isValid($arrParam['name'])){
			$message 	= $validator->getMessages();
			$this->_arrError['name'] 	= current($message);
			$arrParam['name'] 			= '';
		}
		//==========================Validate content============================
		if(!empty($arrParam['content'])):
			$name  = $config->getConfig('content');
			$validator  =  $this->validate($name,$arrParam);
			if(!$validator->isValid($arrParam['content'])){
				$message = $validator->getMessages();
				$this->_arrError['content'] 	= current($message);
				$arrParam['content'] 			= '';
			}
		endif;
		$error = Admin_Form_ProductCategories_ValidateImage::validate($arrParam, $options);
		if($error != ''){
			$this->_arrError['image'] = $error;
			if($arrParam['action'] == 'edit'){
				$arrParam['image'] = $arrParam['current_image'];
			}
		}else{
			if($arrParam['action'] == 'edit'){
				$arrParam['image'] = $arrParam['current_image'];
			}
		}
		$this->_arrData = $arrParam;
	}
	
	public function getData(){
		if(!$this->isVaild()){
			$arrFile = $this->upload();
			if(count($arrFile)>0){
				$this->_arrData = array_merge($this->_arrData,$arrFile);
			}
		}
		return $this->_arrData;
	}
	public function isVaild(){
		$flag = false;
		if(count($this->_arrError) > 0 && $this->_arrError != ''){
			$flag = true;
		}
		return $flag;
	}
	
	public function getMessageErrors(){
		return $this->_arrError;
	}
	public function upload(){
		
		$config = new Zendvn_Controller_Config(array('module'=>$this->_arrData['module'],
												'fileConfig'=>'product-category.ini'));
		$imageConfig 	= $config->getConfig('category');
		
		$upload 	= new Zendvn_File_Upload();
		$fileInfo 	= $upload->getFileInfo('image');
		$fileName 	= $fileInfo['image']['name'];
		$filter 	= new Zendvn_Filter_FileName();
		
		if(!empty($fileName)){
			
			$options 			= array('rename'=> true,'prefix'=>$filter->filter($fileName));
			$fileName 			= $upload->upload($imageConfig['uploadDir'],'image',$options);
			$arrFile['image'] 	= $fileName;
			$img 				= Zendvn_File_Image::create($imageConfig['uploadDir'] . '/' . $fileName);
			
			$img->resize($imageConfig['thumb-width'],$imageConfig['thumb-height'])
				->save($imageConfig['thumbDir']. '/' . $fileName);
			if($this->_arrData['action'] == 'edit'){
				$upload->remove($imageConfig['uploadDir'],$this->_arrData['current_image']);
				$upload->remove($imageConfig['thumbDir'],$this->_arrData['current_image']);
			}
		}
		
		return $arrFile;
	}
}







